package com.iclippi.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;

public class TopPanel extends Composite {

	private HorizontalPanel outer = new HorizontalPanel();
	private HorizontalPanel clipboardNamePanel = new HorizontalPanel();
	private HTML links = new HTML();
	private HorizontalPanel submitPanel = new HorizontalPanel();
	private TextBox clipboardNameField = new TextBox();
	private Button submitButton = new Button("Save");
	private Button clipboardLoadButton = new Button("Load");
	
	
	public void setClipboardName(String clipboardName){
		clipboardNameField.setValue((clipboardName.length()>31)?clipboardName.substring(0, 32):clipboardName);
	}
	public TopPanel(String clipboardName){
		clipboardNameField.setValue((clipboardName.length()>31)?clipboardName.substring(0, 32):clipboardName);
		clipboardNameField.setMaxLength(32);
		clipboardNameField.setTitle("Clipboard name");

		clipboardNamePanel.add(clipboardNameField);
		clipboardNamePanel.add(clipboardLoadButton);
		
		StringBuilder htmlStr = new StringBuilder();
		htmlStr.append("Report a ");
		htmlStr.append("<a target='_BLANK' href=");
		htmlStr.append("'http://code.google.com/p/iclippi/issues/entry?template=Defect%20report%20from%20user'");
		htmlStr.append(">");
		htmlStr.append("bug");
		htmlStr.append("</a>");
		
		htmlStr.append(". Suggest a ");
		htmlStr.append("<a target='_BLANK' href=");
		htmlStr.append("'http://code.google.com/p/iclippi/issues/entry?template=Feature%20request'");
		htmlStr.append(">");
		htmlStr.append("feature");
		htmlStr.append("</a>");
		htmlStr.append(".");
		links.setHTML(htmlStr.toString());
		
		
		submitPanel.add(submitButton);
		submitButton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				String clipboardName = clipboardNameField.getValue();
				Clippy.get().saveClipboard(clipboardName);
			}
			
		});
		
		class ClipboardNameHandler implements ClickHandler, KeyUpHandler {
			@Override
			public void onClick(ClickEvent event) {
				loadClipboard();
			}
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					loadClipboard();
				}
			}
			
			public void loadClipboard(){
				String clipboardName = clipboardNameField.getValue();
				Clippy.get().loadClipboard(clipboardName);
			}
			
		}
		
		ClipboardNameHandler clipboardNameHandler = new ClipboardNameHandler();
		clipboardLoadButton.addClickHandler(clipboardNameHandler);
		clipboardNameField.addKeyUpHandler(clipboardNameHandler);
		
		outer.add(clipboardNamePanel);
		outer.add(submitPanel);
		outer.add(links);
		
		initWidget(outer);
	}

	public void adjustSize(int width, int height) {
		// TODO Auto-generated method stub
		
	}
	
	
}
